package com.bzsg.simpleMediaPlayer.ui.pager.video

import android.app.Activity
import android.os.Build
import android.view.WindowInsets
import android.view.WindowInsetsController
import androidx.annotation.RequiresApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.core.view.WindowCompat

@Composable
fun rememberFullScreenState(): FullScreenState {
    val context = LocalContext.current

    return remember {
        object : FullScreenState {
            @RequiresApi(Build.VERSION_CODES.R)
            override fun enterFullScreen() {
                (context as? Activity)?.let { activity ->
                    WindowCompat.setDecorFitsSystemWindows(activity.window, false)
                    activity.window.insetsController?.let { controller ->
                        controller.hide(WindowInsets.Type.systemBars())
                        controller.systemBarsBehavior =
                            WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                    }
                }
            }

            @RequiresApi(Build.VERSION_CODES.R)
            override fun exitFullScreen() {
                (context as? Activity)?.let { activity ->
                    WindowCompat.setDecorFitsSystemWindows(activity.window, true)
                    activity.window.insetsController?.show(WindowInsets.Type.systemBars())
                }
            }
        }
    }
}

interface FullScreenState {
    fun enterFullScreen()

    fun exitFullScreen()
}
